/** bind*/
/** v1*/
/*!(function (prototype) {
  function bind2(context, ...outerArgs) {
    return (...args) => {
      this/!*因为用了箭头函数 这个this是外部的this*!/.call(context, ...outerArgs, ...args);
    };
  }
 //TODO 处理 new 返回的函数应该能被new (箭头函数无法new)

  prototype.bind2 = bind2;
})(Function.prototype);

const bindGetName = getName2.bind2(obj, 10);
bindGetName('ahhh');*/

// let A = ()=>{};
// console.log(new A()); // TypeError: A is not a constructor


/** v2*/
(function (prototype) {
  prototype.bind = function (context, ...outerArgs) {
    const thatFunc = this; // 缓存当前的函数 Point
    const fBound = function (...args) {
      return thatFunc.apply(
        this instanceof thatFunc ? this : context
        , [...outerArgs, ...args]
      );
    };
    // The Object.create() method creates a new object, using an existing object as the prototype of the newly created object.
    // fBound.prototype = 一个新的object，它的__proto__指向thatFunc.prototype; // 参看 07面向对象/03instanceof
    //↕等价
    fBound.prototype = Object.create(thatFunc.prototype);

    return fBound;
  }
})(Function.prototype);

/*测试用例↓*/
function Point(x, y) {
  this.x = x;
  this.y = y;
}
Point.prototype.toString = function () {
  return this.x + ',' + this.y;
};
const YPoint = Point.bind(null, 1);
const axiosPoint = new YPoint(2);
console.log(axiosPoint.toString()); // 1,2
console.log(axiosPoint instanceof Point); // true
console.log(axiosPoint instanceof YPoint); // true
console.log(axiosPoint); // Point { x: 1, y: 2 }
